import { CACHE_USERINFO } from "@/global/constants"
import type { jumpMode, pageName } from "@/types"
import { localCache } from "@/utils/cacheData"
import { InnerNotify } from "@/utils/notificationHint"
import { h } from "vue"
import { useRouter } from "vue-router"

export default function useRouterJump() {
  const userInfo = localCache.getCache(CACHE_USERINFO)

  const router = useRouter()
  // 在这里写输出是undefined
  // Vue Router 的 useRouter 函数应该在组件的上下文中使用，以获取路由实例。
  // 法一：通过形参传递过来 / 法二：抽取成hooks函数
  function routerJump(mode: jumpMode = "push", page?: pageName | null, argumentData?) {
    if (mode === "back") router[mode]()

    switch (page) {
      case "Login-Register":
        router[mode]("/login")
        break
      case "Register":
        router[mode]("/register")
        break
      case "Home":
        router[mode]("/main/home")
        break
      case "Emulation":
        router[mode]("/main/emulation")
        break
      case "Emulation/emulationHome":
        router[mode]("/emulationHome")
        break
      case "Emulation/simulation":
        router[mode]("/simulation")
        break
      case "Emulation/teaching":
        router[mode]("/teaching")
        break
      case "Emulation/ai-QA":
        router[mode]("/ai-QA")
        break
      case "Monitor":
        router[mode]("/main/monitor")
        break
      case "Databases":
        router[mode]({
          path: "/main/databases",
          query: {
            keyWord: argumentData
          }
        })
        break
      case "AccommodationParticular":
        router[mode]("/main/accommodationParticular")
        break
      case "Release":
        router[mode]("/main/release")
        break
      case "MyAccom":
        router[mode]("/main/myAccom")
        break
      case "Community":
        router[mode]("/main/community")
        break
      case "Community/communityHome":
        router[mode]("/communityHome")
        break
      case "Community/interflow":
        router[mode]("/interflow")
        break
      case "Community/recruitment":
        router[mode]("/recruitment")
        break
      case "Community/workbench":
        router[mode]("/workbench")
        break
      case "AnimalInfoParticulars":
        router[mode]({
          // path: "/animalInfoParticulars",
          // query: {
          //   typeId: 1
          // }
          name: page
          // params: {
          //   animalInfo: JSON.stringify(argumentData)
          // }
        })
        break
      case "ArticleInfoParticulars":
        router[mode]({
          // path: "/animalInfoParticulars",
          // query: {
          //   typeId: 1
          // }
          name: page
          // params: {
          //   animalInfo: JSON.stringify(argumentData)
          // }
        })
        break
      case "PersonalCenter":
        router[mode]("/main/personalCenter")
        break
    }
  }

  function routerJumpByUserInfo() {
    if (!userInfo) {
      routerJump("push", "Login-Register")
    } else {
      routerJump("push", "PersonalCenter")
    }
  }

  /* 没有登录的弹窗 */
  function noLoginInnerNotify() {
    const InnerNotifyInstance = InnerNotify({
      message: h("div", null, [
        h("p", null, "暂未登录，登录完即可进行操作！"),
        h(
          "i",
          {
            onClick: () => {
              routerJump("push", "Login-Register")
              if (InnerNotifyInstance) InnerNotifyInstance.close()
            },
            style: {
              cursor: "pointer",
              color: "#409eff",
              fontWeight: 600,
              borderBottom: "1px solid #409eff"
            }
          },
          "点击登录 >"
        )
      ]),
      type: "info",
      position: "top-right",
      // dangerouslyUseHTMLString: true,
      duration: 3000
    })
  }

  return {
    router,
    userInfo,
    routerJump,
    noLoginInnerNotify,
    routerJumpByUserInfo
  }
}
